package com.joven.product.service.business.domain.model.entity;

import com.joven.infra.common.util.CheckUtils;
import com.joven.product.service.business.domain.model.entity.id.ProductId;
import com.joven.infra.common.model.UniqueIdentifierField;
import com.joven.product.service.business.exception.ExceptionEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 商品领域实体
 * </p>
 *
 * @author Joven
 * @date 2022-01-06 18:21:27
 */
@Getter
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class Product extends BaseDomainObj<ProductId> implements EntityObj<Product> {
    /**
     * 商品名称
     */
    private String name;
    /**
     * 商品编码
     */
    @UniqueIdentifierField
    private String code;

    /**
     * 商品价格
     */
    private BigDecimal price;
    /**
     * 0：待售，1：在售，2：下架
     */
    private Integer status;
    /**
     * 商品分类id
     */
    private Long categoryId;
    /**
     * 供应商id
     */
    private Long supplierId;
    /**
     * 商品详情
     */
    private ProductDetail detail;

    /**
     * 商品属性
     */
    private List<ProductAttribute> attributes;

    public void changePrice(BigDecimal price) {
        if (price == null) {
            return;
        }
        CheckUtils.assertTrue(price.compareTo(BigDecimal.ZERO) > 0, ExceptionEnum.PRODUCT_PRICE_INVALID.createException());
        this.price = price;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Product product = (Product) o;
        return Objects.equals(name, product.name) && ((price == product.price) || (price != null && price.compareTo(product.price) == 0)) && Objects.equals(status, product.status) && Objects.equals(categoryId, product.categoryId) && Objects.equals(supplierId, product.supplierId) && Objects.equals(detail, product.detail) && Objects.equals(attributes, product.attributes);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), name, price, status, categoryId, supplierId, detail, attributes);
    }
}